﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using GBIT.Web;
using WebComponents;
using WebComponents.Security;
using WebComponents.Data;

public partial class ThemSanPhamChietKhau : UserControlBase {
  int discountID = -1;
  protected void Page_Load(object sender, EventArgs e) {
    discountID = ConvertType.ToInt(Request.Params["id"]);
    this.Title = "Chiết khấu";
    this.TaskTitle = "Thêm mới";
    if (discountID > 0) {
      if (!IsPostBack) {
        LoadChietKhau(discountID);
        this.TaskTitle = "Cập nhật";
      }
    }
    this.SaveEnabled = true;
  }

  protected void LoadChietKhau(int Id) {
    ProductDiscountInfo obj = ProductDiscountController.GetByDiscountID(Id);
    txtChietKhau.Text = obj.Amount.ToString();
    txtStartDate.Text = (obj.StartDate.HasValue) ? obj.StartDate.Value.ToShortDateString() : "";
    txtEndDate.Text = (obj.EndDate.HasValue) ? obj.EndDate.Value.ToShortDateString() : "";
    rdPercent.Checked = obj.IsPercent;
  }
  protected override void OnSaveRecord() {
    ProductDiscountInfo obj = null;
    if (discountID > 0)
      obj = ProductDiscountController.GetByDiscountID(discountID);
    if (obj == null)
      obj = new ProductDiscountInfo();
    obj.Amount = ToDecimal(txtChietKhau.Text);
    if (txtStartDate.Text != "")
      obj.StartDate = ToDateTime(txtStartDate.Text);
    if (txtEndDate.Text != "")
      obj.EndDate = ToDateTime(txtEndDate.Text);
    obj.IsPercent = rdPercent.Checked;
    if (discountID > 0) {
      ProductDiscountController.Update(obj);
    } else {
      ProductDiscountController.Insert(obj);
    }
    Security.ShowClientMessage(Page, "Lưu trữ thông tin chiết khấu thành công!", string.Format("?mod={0}", WebComponents.ConvertType.Encode("SanPhamChietKhau/SanPhamChietKhauList.ascx")));
  }
}
